<?php
/**
 * Created by PhpStorm.
 * User: saeb
 * Date: 2/11/19
 * Time: 10:45 AM
 */

function gf_ml_create_db(){
    require_once gf_ml_dir.'configs.php';
    global $wpdb;
    $table_name = gf_ml_tablename();
    $charset = $wpdb->get_charset_collate();

//    err_log('we are in db init');

    $sql = "CREATE TABLE IF NOT EXISTS 
	    $table_name
    	( 
            `id` INT UNSIGNED NOT NULL AUTO_INCREMENT , 
            `form_id` INT UNSIGNED NOT NULL , 
            `limit_number` INT UNSIGNED NOT NULL , 
            `limits_on` TEXT NOT NULL , 
            `is_active` bool DEFAULT TRUE,
            `date_added` TIMESTAMP  DEFAULT CURRENT_TIMESTAMP , 
            `added_by` INT UNSIGNED NULL , 
            PRIMARY KEY (`id`)
        ) 
        $charset;";
    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    dbDelta( $sql );
}
function gf_ml_get_all_results(){
    require_once gf_ml_dir.'configs.php';
    global $wpdb;
    $table_name = gf_ml_tablename();
    $query = "select 
              `id`,
              `form_id`, 
              `limit_number`, 
              `limits_on`
                from $table_name WHERE `is_active` is TRUE ";
    $results = $wpdb->get_results($query,ARRAY_A);
    return $results;
}
function gf_ml_deactive_form_data($form_id){
    require_once gf_ml_dir.'configs.php';
    global $wpdb;
    $table_name = gf_ml_tablename();
    $wpdb->update($table_name,['is_active'=>false],['form_id'=>$form_id]);
}
function gf_ml_deactive_all(){
    require_once gf_ml_dir.'configs.php';
    global $wpdb;
    $table_name = gf_ml_tablename();
    return $wpdb->update($table_name,['is_active'=>false],['is_active'=>true]);
}